package xyz.jangle.jdbc.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * JDBC批量插入
 * @author Administrator 2025年6月24日 09:17:25
 *
 */
public class JdbcBatchInsert {

	public static void main(String[] args) {
		
		String url = "jdbc:mysql://127.0.0.1:3306/jangle_test?characterEncoding=UTF8&serverTimezone=Asia/Shanghai";
		String user = "root";
		String password = "111111";
      
      try(Connection con = DriverManager.getConnection(url, user, password);
    		  PreparedStatement ps = con.prepareStatement("insert into demo(name,age) values (?,?) ")){
    	  
    	  for (int i = 0; i < 3; i++) {
    		  ps.setString(1, "test"+i);
    		  ps.setInt(2, i);
    		  ps.addBatch();
    	  }
    	  int[] batch = ps.executeBatch();
    	  System.out.println("插入"+batch.length+"行");
      } catch (SQLException e) {
		e.printStackTrace();
      }

	}

}
